package com.chaodev.base.controller;

import com.github.pagehelper.PageInfo;
import com.chaodev.base.base.RedisDao;
import com.chaodev.base.dto.MsgDto;
import com.chaodev.base.model.Msg;
import com.chaodev.base.model.Result;
import com.chaodev.base.model.UserInfo;
import com.chaodev.base.service.ILogService;
import com.chaodev.base.service.MsgService;
import com.chaodev.base.utils.JwtUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * vcbase
 *
 * @author 95634
 * @描述
 * @date 2023/5/18 10:05
 **/
@RestController
@RequestMapping("msg")
public class MsgController {
    @Autowired
    MsgService msgService;

    @Autowired
    private RedisDao redisDao;

    @Autowired
    private ILogService logService;

    /**
     * 获取消息推送表列表分页信息
     * @author nw
     */
    @ApiOperation(value = "获取消息推送表列表分页", notes = "获取消息推送表列表分页信息")
    @PostMapping(value = {"list"})
    public Result list(@RequestBody MsgDto dto, HttpServletRequest request) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = null;
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            PageInfo<Msg> msgData = msgService.queryPageList(dto);
            result.setCode(0);
            result.setMsg("操作成功");
            result.setData(msgData);
            logService.logAdd(null, "获取消息推送表列表分页", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(null, "获取消息推送表列表分页", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    /**
     * 获取消息推送表信息
     * @author nw
     */
    @ApiOperation(value = "获取消息推送表对象", notes = "获取消息推送表信息")
    @GetMapping(value = {"/getById"})
    public Result findById(String id, HttpServletRequest request) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = null;
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            Msg dataById = msgService.findDataById(id);
            result.setCode(0);
            result.setMsg("操作成功");
            result.setData(dataById);
            logService.logAdd(null, "获取消息推送表对象", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(null, "获取消息推送表对象", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    /**
     * 保存消息推送表信息
     * @author nw
     */
    @ApiOperation(value = "保存或删除消息推送表对象", notes = "保存或删除消息推送表对象")
    @PostMapping(value = {"/save"})
    public Result save(@RequestBody Msg msg, HttpServletRequest request) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = null;
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            msgService.saveData(msg);
            result.setCode(0);
            result.setMsg("操作成功");
            logService.logAdd(null, "保存或删除消息推送表对象", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(null, "保存或删除消息推送表对象", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    /**
     * 删除消息推送表信息
     * @author nw
     */
    @ApiOperation(value = "删除消息推送表对象", notes = "删除消息推送表信息")
    @GetMapping(value = {"/delete"})
    public Result delete(String id, HttpServletRequest request) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = null;
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            msgService.deleteDataById(id);
            result.setCode(0);
            result.setMsg("操作成功");
            logService.logAdd(null, "删除消息推送表对象", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(null, "删除消息推送表对象", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    /**
     * 删除消息推送表信息
     * @author nw
     */
    @ApiOperation(value = "预览消息", notes = "预览消息")
    @GetMapping(value = {"/read"})
    public Result read(String id, HttpServletRequest request) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = null;
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            msgService.readMsg(id);
            result.setCode(0);
            result.setMsg("操作成功");
            logService.logAdd(null, "预览消息", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(null, "预览消息", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

}
